package com.atguigu.daijia.driver.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.dispatch.client.NewOrderFeignClient;
import com.atguigu.daijia.driver.client.DriverInfoFeignClient;
import com.atguigu.daijia.driver.service.DriverService;
import com.atguigu.daijia.map.client.LocationFeignClient;
import com.atguigu.daijia.model.form.driver.DriverFaceModelForm;
import com.atguigu.daijia.model.form.driver.UpdateDriverAuthInfoForm;
import com.atguigu.daijia.model.vo.driver.DriverAuthInfoVo;
import com.atguigu.daijia.model.vo.driver.DriverLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author 谢忠涵7
 */
@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverServiceImpl implements DriverService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DriverInfoFeignClient driverInfoFeignClient;
    @Autowired
    private LocationFeignClient locationFeignClient;
    @Autowired
    private NewOrderFeignClient newOrderFeignClient;

    /**
     *  司机登录
     */
    @Override
    public String login(String code) {
        //1.远程调用
        Result<Long> loginResult = driverInfoFeignClient.login(code);
        Long driverId = loginResult.getData();
        //2.生成token
        String token = UUID.randomUUID().toString()
                           .replaceAll("-", "");
        //3.把 token 和 司机id 存储在redis 中 并设置过期时间
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX+token,
                                        driverId.toString(),RedisConstant.USER_LOGIN_KEY_TIMEOUT
                                        , TimeUnit.MINUTES);


        return token;
    }
    /**
     *  获取司机登录信息
     */
    @Override
    public DriverLoginVo getDriverLoginInfo(Long driverId) {
        Result<DriverLoginVo> result = driverInfoFeignClient.getDriverLoginInfo(driverId);
        return  result.getData();
    }

    /**
     *  获取司机认证信息
     */
    @Override
    public DriverAuthInfoVo getDriverAuthInfo(Long driverId) {
        Result<DriverAuthInfoVo> result = driverInfoFeignClient.getDriverAuthInfo(driverId);
        return  result.getData();
    }
    /**
     *  更新司机认证信息
     */
    @Override
    public Boolean updateDriverAuthInfo(UpdateDriverAuthInfoForm updateDriverAuthInfoForm) {
        Result<Boolean> result = driverInfoFeignClient.updateDriverAuthInfo(updateDriverAuthInfoForm);
        return  result.getData();
    }

    /**
     * 创建司机人脸模型
     */
    @Override
    public Boolean creatDriverFaceModel(DriverFaceModelForm driverFaceModelForm) {
        Result<Boolean> result = driverInfoFeignClient.creatDriverFaceModel(driverFaceModelForm);
        return  result.getData();
    }

    /**
     * @description: 查询是否进行过人脸识别
     * @author: xiaoxie
     * @date: 2024/10/28 10:40
     * @param: [driverId] 司机id
     * @return:
     **/
    @Override
    public Boolean isFaceRecognition(Long driverId) {
        Result<Boolean> faceRecognition = driverInfoFeignClient.isFaceRecognition(driverId);
        return  faceRecognition.getData();
    }

    /**
     * @description: 通过腾讯云验证司机人脸
     * @author: xiaoxie
     * @date: 2024/10/28 10:57
     * @param: [driverFaceModelForm]
     * @return: java.lang.Boolean
     **/
    @Override
    public Boolean verifyDriverFace(DriverFaceModelForm driverFaceModelForm) {
        Result<Boolean> result = driverInfoFeignClient
                .verifyDriverFace(driverFaceModelForm);
        return  result.getData();
    }
    /**
     * @description: 司机开启接单服务
     * @author: xiaoxie
     * @date: 2024/10/28 11:03
     * @param: [driverId]
     * @return: java.lang.Boolean
     **/
    @Override
    public Boolean startService(Long driverId) {
        //1.身份认证是否成功
        Result<DriverLoginVo> voResult = driverInfoFeignClient.getDriverLoginInfo(driverId);
        if(voResult.getData().getAuthStatus() != 2) {
            throw new GuiguException(ResultCodeEnum.AUTH_ERROR);
        }
        //2.当日是否进行人脸识别
        Result<Boolean> faceRecognition = driverInfoFeignClient.isFaceRecognition(driverId);
        if(!faceRecognition.getData()) {
            throw new GuiguException(ResultCodeEnum.FACE_ERROR);
        }
        //3.更新司机状态
        driverInfoFeignClient.updateServiceStatus(driverId,1);
        //4.删除redis中的位置信息
        locationFeignClient.removeDriverLocation(driverId);
        //5.删除司机的临时队列
        newOrderFeignClient.clearNewOrderQueueData(driverId);
        return true;
    }

    /**
     * @description: 停止接单服务
     * @author: xiaoxie
     * @date: 2024/10/28 11:13
     * @param: [driverId]
     * @return: java.lang.Boolean
     **/
    @Override
    public Boolean stopService(Long driverId) {
        //1.更新状态
        driverInfoFeignClient.updateServiceStatus(driverId,0);
        //4.删除redis中的位置信息
        locationFeignClient.removeDriverLocation(driverId);
        //5.删除司机的临时队列
        newOrderFeignClient.clearNewOrderQueueData(driverId);
        return true;
    }
}
